﻿#include "stdafx.h"
#include "Widget.h"

_NAMESPACE_BEGIN

WidgetWindow::WidgetWindow( CONST_REF(string) name )
{
	window = CEGUI::WindowManager::getSingleton().createWindow((CEGUI::utf8*)"AquaLook/StaticImage", name);
	window->setSize(CEGUI::UVector2(CEGUI::UDim(1, 0), CEGUI::UDim(1, 0)));		//默认为整个窗口用户区大小
	window->setPosition(CEGUI::UVector2(CEGUI::UDim(0, 0), CEGUI::UDim(0, 0)));
	//UiSystem::Instance()->AddWindowFromRoot(window);


}


void WidgetWindow::SetSize( float w, float h )
{
	window->setSize(CEGUI::UVector2(CEGUI::UDim(w, 0), CEGUI::UDim(h, 0)));
}

void WidgetWindow::SetPos( float x, float y )
{
	window->setPosition(CEGUI::UVector2(CEGUI::UDim(x, 0), CEGUI::UDim(y, 0)));
}

void WidgetWindow::SetCenterPos( float x, float y )
{
	float sizeX = window->getSize().d_x.d_scale;	
	float sizeY = window->getSize().d_y.d_scale;

	SetPos(x-sizeX/2,y-sizeY/2);
}

void WidgetWindow::SetClickEnabled( bool val )
{
	window->setRiseOnClickEnabled(val);
}

void WidgetWindow::SetProperty( CONST_REF(string) name, CONST_REF(string) val )
{
	window->setProperty(name,val);
}

void WidgetWindow::SetImage( CONST_REF(string) name )
{
	window->setProperty("Image", 
		CEGUI::PropertyHelper::imageToString(&UiSystem::Instance()->GetImgSet(name)->getImage((CEGUI::utf8*)(name.c_str()))));
}

void WidgetWindow::SetVisible( bool val )
{
	window->setVisible(val);
}

bool WidgetWindow::IsVisible()
{
	return window->isVisible();
}

void WidgetWindow::AddChildWindow( WidgetWindow* win )
{
	this->window->addChildWindow(win->GetWindow());
}

void WidgetWindow::LoadLayout( CONST_REF(string) name )
{
 	window = 
 		CEGUI::WindowManager::getSingleton().loadWindowLayout(
 		(CEGUI::utf8*)(name.c_str())); 

}

void WidgetWindow::SetText( CONST_REF(string) name )
{
	window->setText(name);
}

void WidgetWindow::SetZOrderingEnabled( bool setting )
{
	window->setZOrderingEnabled(setting);
}

void WidgetWindow::Activate()
{
	window->activate();
}
//////////////////////////////////////////////////////////////////////////

WidgetButton::WidgetButton(CONST_REF(string) text, CONST_REF(string) name)
{
	buttonText = text;

 	window = (CEGUI::PushButton*)CEGUI::WindowManager::getSingleton().createWindow
 		("AquaLook/Button", (CEGUI::utf8*)(name.c_str()));
 	//UiSystem::Instance()->AddWindowFromRoot(window);
	//默认大小和位置
 	window->setSize(CEGUI::UVector2(CEGUI::UDim(0.2f, 0), CEGUI::UDim(0.1f, 0)));
 	window->setPosition(CEGUI::UVector2(CEGUI::UDim(0, 0), CEGUI::UDim(0, 0)));
 	window->setText((CEGUI::utf8*)(buttonText.c_str()));

	window
		->subscribeEvent(
		CEGUI::PushButton::EventClicked, 
		CEGUI::Event::Subscriber(&WidgetButton::Event, this));
}

WidgetButton::WidgetButton( CEGUI::Window* win )
{
	assert(win);
	window = (win);

	window
		->subscribeEvent(
		CEGUI::PushButton::EventClicked, 
		CEGUI::Event::Subscriber(&WidgetButton::Event, this));
}

bool WidgetButton::Event(const CEGUI::EventArgs& e)
{
	//执行脚本被观察者方法
	clickSlot();
	return true;	
}

//////////////////////////////////////////////////////////////////////////
WidgetRadioButton::WidgetRadioButton(const std::string &name)
{

	window = (CEGUI::PushButton*)CEGUI::WindowManager::getSingleton().createWindow
		("AquaLook/RadioButton", (CEGUI::utf8*)(name.c_str()));
	//UiSystem::Instance()->AddWindowFromRoot(window);
	//默认大小和位置
	window->setSize(CEGUI::UVector2(CEGUI::UDim(0.2f, 0), CEGUI::UDim(0.1f, 0)));
	window->setPosition(CEGUI::UVector2(CEGUI::UDim(0, 0), CEGUI::UDim(0, 0)));

	window
		->subscribeEvent(
		CEGUI::RadioButton::EventSelectStateChanged, 
		CEGUI::Event::Subscriber(&WidgetRadioButton::Event, this));

}

WidgetRadioButton::WidgetRadioButton(CEGUI::Window *win)
{
	assert(win);
	window = (win);

	window
		->subscribeEvent(
		CEGUI::RadioButton::EventSelectStateChanged, 
		CEGUI::Event::Subscriber(&WidgetRadioButton::Event, this));
}

bool WidgetRadioButton::Event(const CEGUI::EventArgs& e)
{
	//执行脚本被观察者方法
	clickSlot();
	return true;
}

//////////////////////////////////////////////////////////////////////////

WidgetEditBox::WidgetEditBox(const std::string &name)
{
	assert(!"未重写此方法");
}

WidgetEditBox::WidgetEditBox(CEGUI::Window *win)
{
	assert(win);
	window = (win);
	window
		->subscribeEvent(
		CEGUI::Editbox::EventTextAccepted, 
		CEGUI::Event::Subscriber(&WidgetEditBox::Event, this));
}

void WidgetEditBox::setTextMasked(bool setting)
{
	static_cast<CEGUI::Editbox*>(window)->setTextMasked(setting);
}

void WidgetEditBox::setValidationString(const String &validation_string)
{
	static_cast<CEGUI::Editbox*>(window)->setValidationString(validation_string);
}

bool WidgetEditBox::Event(const CEGUI::EventArgs& e)
{
	return true;
}

//////////////////////////////////////////////////////////////////////////

WidgetListBox::WidgetListBox( CONST_REF(string) name )
{
	assert(!"未重写此方法");
}

WidgetListBox::WidgetListBox( CEGUI::Window* win )
{
	assert(win);
	window = (win);
	window
		->subscribeEvent(
		CEGUI::Listbox::EventSelectionChanged, 
		CEGUI::Event::Subscriber(&WidgetListBox::ChangeSelEvent, this));
}

bool WidgetListBox::ChangeSelEvent( const CEGUI::EventArgs& e )
{
	//执行脚本被观察者方法
	changeSelSlot();
	return true;
}

int WidgetListBox::GetSelectedCount()
{
	return static_cast<CEGUI::Listbox*>(window)->getSelectedCount();
}

void WidgetListBox::GetCurtSel(string& text)
{
	CEGUI::ListboxItem* item = static_cast<CEGUI::Listbox*>(window)->getFirstSelectedItem();
	if(item)
	{
		text = item->getText().c_str();
	}
}

void WidgetListBox::AddItem( CONST_REF(string)text, int id )
{
	CEGUI::ListboxTextItem* itemText = new CEGUI::ListboxTextItem((CEGUI::utf8*)(text.c_str()), id);
	itemText->setSelectionBrushImage("TaharezLook", "ListboxSelectionBrush");
	static_cast<CEGUI::Listbox*>(window)->addItem(itemText);
}

void WidgetListBox::ClearItems()
{
	//static_cast<CEGUI::Listbox*>(window)->clearAllSelections();
	static_cast<CEGUI::Listbox*>(window)->resetList();
}

//////////////////////////////////////////////////////////////////////////

WidgetComboBox::WidgetComboBox( CONST_REF(string) name )
{
	assert(!"未重写此方法");
}

WidgetComboBox::WidgetComboBox( CEGUI::Window* win )
{
	assert(win);
	window = (win);
 	window
 		->subscribeEvent(
 		CEGUI::Combobox::EventListSelectionAccepted, 
 		CEGUI::Event::Subscriber(&WidgetComboBox::ChangeSelEvent, this));
}

bool WidgetComboBox::ChangeSelEvent( const CEGUI::EventArgs& e )
{
	changeSelSlot();
	return true;
}

void WidgetComboBox::ClearItems()
{
	/*static_cast<CEGUI::Combobox*>(window)->clearAllSelections();*/
	static_cast<CEGUI::Combobox*>(window)->resetList();
}

void WidgetComboBox::AddItem( CONST_REF(string)text, int id )
{
	CEGUI::ListboxTextItem* itemText = new CEGUI::ListboxTextItem((CEGUI::utf8*)(text.c_str()), id);
	static_cast<CEGUI::Combobox*>(window)->addItem(itemText);
}

void WidgetComboBox::GetCurtSel( string& text )
{
	text = static_cast<CEGUI::Combobox*>(window)->getSelectedItem()->getText().c_str();
}

//////////////////////////////////////////////////////////////////////////

WidgetMultilineList::WidgetMultilineList( CONST_REF(string) name )
{
	assert(!"未重写此方法");
}

WidgetMultilineList::WidgetMultilineList( CEGUI::Window* win )
{
	assert(win);
	window = (win);
	static_cast<CEGUI::MultiColumnList*>(window)->setSelectionMode(CEGUI::MultiColumnList::RowSingle);
}

void WidgetMultilineList::SetRowItem( CONST_REF(string)text, int row, int col )
{
	CEGUI::ListboxTextItem* itemText = new CEGUI::ListboxTextItem((CEGUI::utf8*)(text.c_str()));
	itemText->setSelectionBrushImage("TaharezLook", "ListboxSelectionBrush");
	static_cast<CEGUI::MultiColumnList*>(window)->setItem(itemText,col,row);
		
}

void WidgetMultilineList::ClearItems()
{
	static_cast<CEGUI::MultiColumnList*>(window)->clearAllSelections();
	static_cast<CEGUI::MultiColumnList*>(window)->resetList();
}

void WidgetMultilineList::AddRow( int row )
{
 	static_cast<CEGUI::MultiColumnList*>(window)->addRow(row);
}

void WidgetMultilineList::GetCurtSel( string& text, int colOff )
{
	CEGUI::MultiColumnList* mclBox = static_cast<CEGUI::MultiColumnList*>(window);

	if (mclBox->getSelectedCount() <= 0)
	{
		return;
	}

	CEGUI::MCLGridRef girdRef = mclBox->getItemGridReference(mclBox->getFirstSelectedItem());
	girdRef.column += colOff;
	CEGUI::ListboxItem* itemText = mclBox->getItemAtGridReference(girdRef);
	text.clear();
	text = itemText->getText().c_str();
}

int WidgetMultilineList::GetSelectedCount()
{
	return static_cast<CEGUI::MultiColumnList*>(window)->getSelectedCount();
}

_NAMESPACE_END